
//
//  PatientreCodsViewController.m
//  EcmDoctor
//
//  Created by Mac on 16/2/24.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "PatientreCodsViewController.h"
#import "AppUtils.h"
#import "PatientreCodsm.h"
#import "PatientreCodsTableViewCell.h"
#import "UIImageView+AFNetworking.h"
#import "PDC.h"
#import "DetailPcViewController.h"
@interface PatientreCodsViewController ()<UITableViewDataSource,UITableViewDelegate>

{
    NSMutableArray *_dataAry;
    __weak IBOutlet UITableView *_pcTv;
}
@end

@implementation PatientreCodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.navigationItem.titleView=[AppUtils generateNavigationItemTitleViewWithTitle:@"患者病历处方"];
   
    self.view.backgroundColor = [AppUtils getBackground];
    UIImage *tableIcon = [UIImage imageNamed:@"btn_back"];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithImage:tableIcon style:UIBarButtonItemStylePlain target:self action:@selector(changeToNavRootController:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithPatternImage:tableIcon];
    _pcTv.separatorStyle=UITableViewCellSeparatorStyleNone;
    _pcTv.rowHeight=100;
    //请求数据
    [self reloadtableView];
    
}
-(void)reloadtableView
{
    NSString *docId= [SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY];
    [PatientreCodsm requetDoctorpcWith:docId success:^(NSArray *pcAry) {
        _dataAry=[NSMutableArray arrayWithArray:pcAry];
        [_pcTv reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
-(void)changeToNavRootController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataAry.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    PatientreCodsm *patientreCodsm=_dataAry[indexPath.row];
    PatientreCodsTableViewCell *patientreCodsCell=[tableView dequeueReusableCellWithIdentifier:@"patientrecods" forIndexPath:indexPath];
       
    [patientreCodsCell.iconImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ecm-user.oss-cn-beijing.aliyuncs.com/image/%@",patientreCodsm.user_icon]] placeholderImage:[UIImage imageNamed:@"icon"]];
    patientreCodsCell.customerName.text=patientreCodsm.name;
    patientreCodsCell.customerSex.text=[NSString stringWithFormat:@"性别：%@",patientreCodsm.sex];
    patientreCodsCell.customerAge.text=[NSString stringWithFormat:@"年龄：%@",patientreCodsm.age];
    patientreCodsCell.date.text=[NSString stringWithFormat:@"最近一次问诊时间：%@",patientreCodsm.max_date];
    
    cell=patientreCodsCell;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     PatientreCodsm *patientreCodsm=_dataAry[indexPath.row];
    NSString *docId= [SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY];
    [PDC requetDoctorpcWith:docId And:patientreCodsm.user_id success:^(NSArray *pdcAry) {
        [self performSegueWithIdentifier:@"detalcodes" sender:pdcAry];
        NSLog(@"-----------------%@",pdcAry);
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
        
    }
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"detalcodes"]) {
        DetailPcViewController* depc = [segue destinationViewController];
        
        depc.dataAry  =[NSArray arrayWithArray:sender];
    }
    
}


@end
